<template>
	<view>
		<view class="hander-tab">
			<view @click="back" class="hander-icon">
				<u-icon class="icon1" name="arrow-left" color="#0f0f0f" size="25"></u-icon>
			</view>
			<view class="hander-text">
				发布动态
			</view>
		</view>
		<!-- 内容编辑区 -->
		<view class="" style="padding: 30rpx;">
			<textarea v-model="articletitle" style="width: 100%;height: 500rpx;" name="" id="" cols="30" rows="10"
				placeholder="记录一个角落"></textarea>
			<view class="" style="display: flex;flex-wrap: wrap;">
				<view @click.stop="previewimg(index)" v-if="fileList.length" v-for="(item,index) in fileList" class=""
					style="width: 160rpx;height: 160rpx;background-color: #f4f5f7;display: flex;align-items: center;justify-content: center;border-radius: 5rpx;margin-right: 20rpx;margin-bottom: 20rpx;position: relative;">
					<image :src="item" style="width: 160rpx;height: 160rpx;border-radius: 5rpx;" mode="aspectFill">
					</image>
					<view class="" style="position: absolute;top: 0;right: 0;">
						<u-icon @click="deleteimg(index)" name="trash-fill" color="#2979ff" size="28"></u-icon>
					</view>
				</view>
				<view @click="uploadImages" class=""
					style="width: 160rpx;height: 160rpx;background-color: #f4f5f7;display: flex;align-items: center;justify-content: center;border-radius: 5rpx;">
					<u-icon name="photo" color="#d3d4d6" size="28"></u-icon>
				</view>
			</view>
			<view class="common-post-labe">
				<text @click="changelabel" class="topic">{{label_data?label_data.label_name:'请选择一个分类'}}</text>
			</view>
			<!-- 分类列表选择器 -->
			<u-picker :show="labeshow" :columns="labecolumns" :closeOnClickOverlay="true" @close="labeshow=false"
				@cancel="labeshow=false" @confirm="confirmlabe" keyName="label_name"></u-picker>
		</view>
		<!-- 发布按钮 -->
		<view class=""
			style="display: flex;align-items: center;justify-content: center;position: fixed;bottom: 200rpx;width: 100%;">
			<view @click="addarticle" class=""
				style="width: 90%;height: 100rpx;border-radius: 10rpx;background-color: #3668fc;display: flex;align-items: center;justify-content: center;">
				<text style="color: #ffffff;">发布</text>
			</view>
		</view>
	</view>
</template>

<script>
	//引入域名
	import url from '@/commom/config.js';
	export default {
		data() {
			return {
				labeshow: false,
				labecolumns: [],
				label_data: null,
				file: null,
				fileList: [],
				fileLists: [],
				articletitle: null,
				user_id: null,
			}
		},
		onLoad() {
			uni.getStorage({
				key: 'userinfo',
				success: (res) => {
					this.user_id = res.data.id
				}
			})
			this.getlabellist()
		},
		methods: {
			//查询所有标签分类列表
			getlabellist() {
				this.$http({
					url: '/api/getlabellist'
				}).then(res => {
					console.log(res);
					this.labecolumns[0] = res
				})
			},
			//选择值
			confirmlabe(e) {
				this.labeshow = false
				this.label_data = e.value[0]
			},
			//打开分类列表选择器
			changelabel() {
				this.labeshow = true
			},
			back() {
				uni.navigateBack()
			},
			//删除fileList里指定的图片
			deleteimg(index) {
				this.fileList.splice(index, 1);
			},
			//预览图片
			previewimg(i) {
				uni.previewImage({
					current: i,
					urls: this.fileList
				})
			},
			uploadImages(event) {
				uni.chooseImage({
					success: (res) => {
						this.fail = res.tempFilePaths
						this.fileList.push(...this.fail)
					}
				})
			},
			//确认发布
			async addarticle() {
				if (this.articletitle == null) {
					return uni.showToast({
						title: "说点什么呀",
						icon: 'none'
					})
				}
				for (let i = 0; i < this.fileList.length; i++) {
					try {
						const res = await new Promise((resolve, reject) => {
							uni.uploadFile({
								url: url.BASE_URL+'/uploadfile/upload_img',
								filePath: this.fileList[i],
								name: 'image',
								formData: {
									image: this.fileList[i]
								},
								success: (res) => {
									resolve(res.data)
								},
								fail: (err) => {
									reject(err)
								}
							})
						})
						this.fileLists.push(res)
					} catch (err) {
						console.log(err)
					}
				}
				this.$http({
					url: '/uploadfile/upload_article',
					method: 'POST',
					header_type:3,
					data: {
						article_title: this.articletitle,
						article_author_id: this.user_id,
						article_pic: this.fileLists.length ? JSON.stringify(this.fileLists) : null,
						label: this.label_data?this.label_data.id:null
					},
				}).then(res=>{
					console.log(res);
					if (res.status == 0) {
						uni.showToast({
							title: "发布成功,审核中",
							icon: 'none',
							success: (res) => {
								this.fileLists = []
								this.articletitle = ""
								setTimeout(res => {
									uni.reLaunch({
										url: '/pages/index/index'
									})
								}, 500)
							}
						})
					} else {
						uni.showToast({
							title: "发布失败",
							icon: 'error'
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.common-post-labe {
		margin-top: 50rpx;
		font-size: 24rpx;
	}

	.topic {
		color: #595959;
		background-color: #f5f5f5;
		margin: 0 10rpx 0 0;
		padding: 10rpx 20rpx;
		border-radius: 10rpx;
	}

	.hander-tab {
		position: sticky;
		z-index: 99;
		top: 0;
		display: flex;
		justify-content: center;
		/* #ifdef MP-WEIXIN */
		padding: 80rpx 20rpx 0rpx 20rpx;
		/* #endif */
		/* #ifndef MP-WEIXIN */
		padding: 0 20rpx;
		/* #endif */
		height: 100rpx;
		align-items: center;
		background-color: #ffffff;
	}

	.hander-text {
		font-size: 40rpx;
	}

	.hander-icon {
		display: flex;
		position: absolute;
		left: 10rpx;
	}

	.hander-icon .icon1 {
		// margin-left: 30rpx;
	}
</style>